import { observable, computed } from 'mobx';
import { Promise } from 'drunk-base';
import { updateEvent, createEvent, getEvent } from './EventHttpResource';
import { isNumber, isUrl, notEmpty } from '../../../service/Validator';
import { ItemModel, IItemProps, Validate } from '../../ItemModel';

export interface IEventModel extends IItemProps {
    name: string;
    coverImageUrl: string; // 封面图片
    hostName: string; // 主办方/店铺名
    district?: string; // 区域
    url: string; // 活动跳转链接
    price?: {// 现价
        value: number; // 价格
        unit: string; // 单位
    };
    originalPrice?: number;
    tags?: string[];
    isOutLink: boolean; // 是否是外链

    outLinkDesc?: string; // 外链显示的描述
    likeCount?: string; // 喜欢人数
    slides?: string[]; // 轮播图
    tel?: string;
    address?: string;
    coordinates?: [string, string];
    recommendReason?: string; // html
    content?: string; // 其他html
}

export class EventModel extends ItemModel<IEventModel> {

    @observable data: IEventModel = {
        // 基础信息
        name: "",
        coverImageUrl: "",
        hostName: "",
        district: "",
        price: { value: null, unit: "" },
        originalPrice: null,
        tags: [""],
        url: "",
        isOutLink: true,

        // 内容编辑
        slides: [],
        outLinkDesc: "",
        likeCount: "",
        tel: "",
        address: "",
        coordinates: null,
        content: "",
        recommendReason: "",
    }

    @Validate(`请输入活动名称`)
    @computed get nameValid() { return notEmpty(this.data.name) }

    @Validate(`请上传一张活动封面图片`)
    @computed get coverImageUrlValid() { return notEmpty(this.data.coverImageUrl) }

    @Validate(`请输入主办方或店铺名称`)
    @computed get hostNameValid() { return notEmpty(this.data.hostName) }

    @Validate(`请输入合法的跳转链接`)
    @computed get urlValid() { return isUrl(this.data.url) }

    @Validate(`不能有空标签`)
    @computed get tagsValid() { return this.data.tags.length >= 1 && this.data.tags.every(tag => notEmpty(tag)) }

    @computed get outLinkDescValid() { return notEmpty(this.data.outLinkDesc) }
    @computed get likeCountValid() { return isNumber(this.data.likeCount) }
    @computed get recommendReasonValid() { return notEmpty(this.data.recommendReason) }
    @computed get slidesValid() { return this.data.slides.length >= 1 && this.data.slides.every(slide => notEmpty(slide)) }

    fetchItemImpl(id: string) {
        return getEvent(id);
    }

    createItemImpl(data: IEventModel) {
        return createEvent(data);
    }

    updateItemImpl(id: string, data: IEventModel) {
        return updateEvent(id, data);
    }

    addTagAt(position: number) {
        this.data.tags.splice(position, 0, "");
    }

    removeTagAt(position: number) {
        this.data.tags.splice(position, 1);
    }

    validateExcept() {
        let success = false;
        let message = "";

        if (!this.data.isOutLink) {
            if (!this.outLinkDescValid) {
                message = `请输入跳转按钮名称`;
                return { success, message };
            }
            if (!this.likeCountValid) {
                message = `请输入喜欢人数`;
                return { success, message };
            }
            if (!this.recommendReasonValid) {
                message = `请完善推荐理由`;
                return { success, message };
            }
        }
        
        return super.validateExcept();
    }
}